<template>
  <div class="channel-edit">
    <!-- 当前登陆用户已经订阅的频道 -->
    <div class="channel">
      <van-cell title="我的频道" :border="false">
        <van-button
          size="small"
          type="primary"
          plain
          @click="isEditing = !isEditing"
          >{{ isEditing ? "保存" : "编辑" }}</van-button
        >
      </van-cell>
      <van-grid>
        <van-grid-item
          @click="hChangeChannel(index)"
          v-for="(channel, index) in channels"
          :key="channel.id"
          :class="{ red: index === curIndex }"
        >
          <span>{{ channel.name }}</span>
          <van-icon
            name="cross"
            class="btn"
            v-if="isEditing && index.id != 0"
          ></van-icon>
        </van-grid-item>
      </van-grid>
    </div>
    <!-- 当前登陆用户没有订阅的频道 -->
    <div class="channel">
      <van-cell title="可选频道" :border="false"></van-cell>
      <van-grid>
        <van-grid-item
          v-for="channel in recommendChannels"
          :key="channel.id"
          @click="hAddChannel(channel)"
        >
          <span>{{ channel.name }}</span>
        </van-grid-item>
      </van-grid>
    </div>
  </div>
</template>

<script>
// import { arrSub } from '../../utils/Channels'
import { getAllChannels, addChannels, deleChannel } from '../../api/channel'
export default {
  name: 'ChannelEdit',
  props: ['channels', 'curIndex'],
  data () {
    return {
      allChannels: [],
      isEditing: false // 是否处于编辑状态
    }
  },
  created () {
    this.loadAllChannels()
  },
  methods: {
    // 删除频道
    async doDelete (index) {
      const channelId = this.channels[index].id
      // 就做删除
      try {
        const res = await deleChannel(channelId)
        console.log(res)
        // 我的频道 少1项， 删除当前项
        this.channels.splice(index, 1)


        // 如果要删除的频道下标小于当前的频道下标
        // 通知父组件修改active
        if (index < this.curIndex) {
          this.$emit('update-active')
        }
        this.$toast.success('删除成功')
      } catch (err) {
        console.log(err)
      }
    },
    // 用户点击了我的频道
    hChangeChannel (index) {
      if (this.isEditing) {
        // 删除
        this.doDelete(index)
      } else {
        // 当前频道的下标
        this.$emit('change-channel', index)
      }
    },
    async loadAllChannels () {
      try {
        const res = await getAllChannels()
        this.allChannels = res.data.data.channels
      } catch (err) {
        console.log(err)
      }
    },
    // 添加频道
    async hAddChannel (channel) {
      try {
        await addChannels([channel])

        // 添加我的频道
        this.channels.push(channel)
      } catch (err) {
        console.log(err)
      }
    }


  },
  // 根据已有数据生成新的数据 ---- 计算属性
  computed: {
    recommendChannels () {
      // 已有频道： [{id:1,name:'a'}]
      // 所有频道： [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'}]

      // 推荐频道： 所有频道 - 已有频道  ===> [{id:2,name:'b'},{id:3,name:'c'}]

      // 对所有频道的数据进行过滤：
      //   保存数据的条件是：当前这个频道没有在已有频道中出现
      const arr = this.allChannels.filter(channel => {
        // eslint-disable-next-line eqeqeq
        const idx = this.channels.findIndex(item => item.id == channel.id)
        if (idx === -1) {
          return true
        }
      })
      return arr
      // return this.allChannels - this.channels
    }
  }
}
</script>

<style lang="less" scoped>
.channel {
  padding: 15px;
  font-size: 14px;
}
.btn {
  position: absolute;
  top: 0;
  right: 0;
  font-size: 24px;
}
// 高亮显示
.red {
  color: rgba(39, 223, 122, 0.911);
  font-weight: bold;
}
</style>
